package chixing.day20.work5;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DispatchService {
    private List<Rider> riders;
    private List<Order> pendingOrders;
    private List<Store> stores;
    private Map<Rider, List<Order> > riderOrders;
    public void addOrder(Order order) {
        // 添加订单到待处理订单列表
        pendingOrders.add(order);
    }
    public void addRider(Rider rider) {
        // 添加骑手到骑手列表
        riders.add(rider);
    }
    public Rider dispatch(Order order) {
        // 获取可用骑手
        List<Rider> availableRiders = getAvailableRiders();
        if (availableRiders.isEmpty()) {
            System.out.println("订单ID: O" + String.format("%03d", order.getOrderId()) +
                    " 暂无可用骑手，进入等待队列");
            return null;
        }
        return null;
    }

    public void finishDelivery(Rider rider, Order order) {
        if (riderOrders.containsKey(rider) && riderOrders.get(rider).contains(order)) {
            order.completeDelivery();
            rider.completeOrder();
            riderOrders.get(rider).remove(order);
            System.out.println("骑手 " + rider.getRiderName() +
                    " 完成订单 O" + String.format("%03d", order.getOrderId()));
        }
    }
    public List<Order> getOrdersByRider(Rider rider) {
        return riderOrders.getOrDefault(rider, new ArrayList<>());
    }
    public List<Rider> getAvailableRiders() {
        return riders.stream()
                .filter(Rider::canAcceptOrder)
                .collect(Collectors.toList());
    }
}
